package com.uchengwang.ui.cm_ui.me.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.umsg.bean.SchoolClubBean;
import com.uchengwang.ui.utils.SwitchAccountUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/6/8.
 */

public class MyAccountAdapter extends RecyclerView.Adapter<MyAccountAdapter.MyAccountVH>{
    private Context context;
    private List<SchoolClubBean> list = new ArrayList<>();
    public MyAccountAdapter(Context context){
        this.context = context;

    }

    public void setData(List<SchoolClubBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<SchoolClubBean> getData(){
        return list;
    }

    @Override
    public MyAccountVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_my_account, parent, false);
        return new MyAccountVH(view);
    }

    @Override
    public void onBindViewHolder(MyAccountVH holder, int position) {
        final SchoolClubBean bean = list.get(position);
        final boolean isMyCreate = bean.isMyCreate();//true是自己创建的社团

        if (TextUtils.isEmpty(bean.getLogoImg())){
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, bean.getLogoImg(), ImageManager.ImageType.CIRCLE);
        }

        holder.name.setText(TextUtils.isEmpty(bean.getOrgName()) ? "暂无社团名" : bean.getOrgName());
        holder.account_sign.setText(TextUtils.isEmpty(bean.getOrgSign()) ? "暂无个性签名" : bean.getOrgSign());

        if (isMyCreate){
            holder.admin.setText("(社长)");
            holder.admin.setTextColor(context.getResources().getColor(R.color.ucw_color));
        }else {
            holder.admin.setText("(成员)");
            holder.admin.setTextColor(context.getResources().getColor(R.color.color_location));
        }

        if (bean.isRecruitUnread()){
            //本社团有未读的纳新报名。留言
            holder.noRead.setVisibility(View.VISIBLE);
        }else {
            holder.noRead.setVisibility(View.GONE);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMyCreate){
                    SwitchAccountUtils.getUtils().toClubAccount(context, bean);
                }else {
                    //成员跳转到社团主界面
//                    Intent intent = new Intent(context, ClubDetailActivity.class);
//                    intent.putExtra(BaseConfig.CLUB_ID, bean.getUid());
//                    context.startActivity(intent);
                }

            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyAccountVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView head;
        private TextView name, admin, noRead, account_sign;
        
        public MyAccountVH(View view) {
            super(view);
            AutoUtils.autoSize(view);

            itemView = view;
            head = (ImageView) view.findViewById(R.id.account_head);
            name = (TextView) view.findViewById(R.id.account_name);
            admin = (TextView) view.findViewById(R.id.account_admin);
            account_sign = (TextView) view.findViewById(R.id.account_sign);
            noRead = (TextView) view.findViewById(R.id.account_noRead);
        }
    }
}
